import {isUrl} from "./utils/utils"
import Sub1 from './page/Home/Sub1'
import About from './page/About'

const menuData = [
  {
    name: 'Home',
    path: 'home',
    icon: 'home',
    children: [
      {
        name: 'Sub1',
        path: 'sub1',
        icon: 'star',
        component: Sub1,
      },
    ],
  },
  {
    name: 'About',
    path: 'about',
    children: [
      {
        name: 'Sub1',
        path: 'sub1',
        component: About,
      },
      {
        name: 'Sub2',
        path: 'sub2',
        component: About,
      },
    ],
  },
]

function formatter(data, parentPath = '/', parentAuthority) {
  return data.map(item => {
    let { path } = item
    if (!isUrl(path)) {
      path = parentPath + item.path
    }
    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority,
    }
    if (item.children) {
      result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority)
    }
    return result
  })
}

const getMenucomponentData = (menus, routerData) => {
  menus.forEach(item => {
    if (item.children) {
      getMenucomponentData(item.children, routerData)
    } else if (item.component) {
      routerData.push(item)
    }
  })
  return routerData
}

export const menuRouterData = getMenucomponentData(formatter(menuData), [])

export const getMenuData = () => formatter(menuData)
